package top.lixunda.ecommerce.server.good.api.entity.vo;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodOrderDO;
import top.lixunda.ecommerce.server.good.api.entity.db.OrderDetailDO;
import top.lixunda.ecommerce.server.good.api.entity.db.SubOrderDO;

import java.util.*;

/**
 * @author Xumda
 * @version time:2019/12/18 17:26
 * <p>
 * 手动级联，一个GoodOrderDO中包含多个SubOrderDO数据
 */
@SuppressWarnings({"WeakerAccess", "unused"})
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class OrderConvertVO extends GoodOrderDO {

    /**
     * 订单项数据
     */
    private List<SubOrderDO> list;

    /**
     * 转换为GoodOrderDO
     *
     * @param orderDetailDO OrderDetailDO
     * @return OrderConvertVO
     */
    public static OrderConvertVO convertSimpleData(OrderDetailDO orderDetailDO) {
        OrderConvertVO dto = new OrderConvertVO();
        dto.setId(orderDetailDO.getOrderId()).setStoreId(orderDetailDO.getStoreId())
                .setStoreName(orderDetailDO.getStoreName()).setCreateTime(orderDetailDO.getCreateTime())
                .setPriceTime(orderDetailDO.getPriceTime()).setGoodTime(orderDetailDO.getGoodTime())
                .setEndTime(orderDetailDO.getEndTime()).setGoodsPrice(orderDetailDO.getGoodsPrice())
                .setFreight(orderDetailDO.getTotalFreight()).setHaveFreightInsu(orderDetailDO.getHaveFreightInsu())
                .setFreightInsu(orderDetailDO.getFreightInsu()).setRedEnvelope(orderDetailDO.getRedEnvelope())
                .setIntegral(orderDetailDO.getTotalIntegral()).setPrice(orderDetailDO.getTotalPrice())
                .setPayType(orderDetailDO.getPayType()).setPayId(orderDetailDO.getPayId()).setLogisticsCode(orderDetailDO.getLogisticsCode())
                .setLogisticsNumber(orderDetailDO.getLogisticsNumber()).setLogisticsType(orderDetailDO.getLogisticsType())
                .setStartAddressId(orderDetailDO.getStartAddressId()).setEndAddressId(orderDetailDO.getEndAddressId())
                .setStartAddressDesc(orderDetailDO.getStartAddressDesc()).setEndAddressDesc(orderDetailDO.getEndAddressDesc())
                .setState(orderDetailDO.getOrderState()).setMessage(orderDetailDO.getMessage())
                .setUseable(orderDetailDO.getUseable());
        return dto;
    }

    /**
     * 转换为SubOrderDO
     *
     * @param orderDetailDO OrderDetailDO
     * @return SubOrderDO
     */
    public static SubOrderDO convertSubOrder(OrderDetailDO orderDetailDO) {
        SubOrderDO sub = new SubOrderDO();
        sub.setId(orderDetailDO.getSubId()).setGoodId(orderDetailDO.getGoodId()).setGoodName(orderDetailDO.getGoodName())
                .setSubGoodId(orderDetailDO.getSubGoodId()).setSubGoodName(orderDetailDO.getSubGoodName())
                .setPrice(orderDetailDO.getSubPrice()).setTotal(orderDetailDO.getSubTotal())
                .setCount(orderDetailDO.getSubCount()).setFreight(orderDetailDO.getSubFreight())
                .setOtherPreferential(orderDetailDO.getOtherPreferential())
                .setOtherPreferentialDesc(orderDetailDO.getOtherPreferentialDesc())
                .setIntegral(orderDetailDO.getSubIntegral()).setRemark(orderDetailDO.getSubRemark());
        return sub;
    }

    /**
     * 从OrderDetailDO列表中进行级联相似的转换
     *
     * @param orderDetailList List<OrderDetailDO>
     * @return OrderDetailConvertDTO列表
     */
    public static List<OrderConvertVO> fromOrderDetailDO(List<OrderDetailDO> orderDetailList) {
        if (orderDetailList == null) {
            return null;
        }
        if (orderDetailList.size() == 0) {
            return Collections.emptyList();
        }
        // 保证顺序
        Set<Integer> set = new LinkedHashSet<>(10);
        Map<Integer, OrderConvertVO> orderCache = new HashMap<>(orderDetailList.size() / 2);
        OrderConvertVO detailConvertDTO;
        for (OrderDetailDO orderDetailDO : orderDetailList) {
            detailConvertDTO = orderCache.get(orderDetailDO.getOrderId());
            if (detailConvertDTO == null) {
                detailConvertDTO = convertSimpleData(orderDetailDO);
                detailConvertDTO.setList(new ArrayList<>(8));
                set.add(orderDetailDO.getOrderId());
                orderCache.put(orderDetailDO.getOrderId(), detailConvertDTO);
            }
            detailConvertDTO.getList().add(convertSubOrder(orderDetailDO));
        }
        List<OrderConvertVO> list = new ArrayList<>(set.size());
        for (Integer orderId : set) {
            list.add(orderCache.get(orderId));
        }
        return list;
    }

    /**
     * 从OrderDetailDO列表中进行级联相似的转换
     *
     * @param orderDetailList List<OrderDetailDO>
     * @return OrderConvertVO
     */
    public static OrderConvertVO fromOrderDetailOnlyOne(List<OrderDetailDO> orderDetailList) {
        List<OrderConvertVO> orderDetailConverts = fromOrderDetailDO(orderDetailList);
        if (orderDetailConverts == null || orderDetailConverts.size() == 0) {
            return null;
        }
        return orderDetailConverts.get(0);
    }
}
